<template>
    <el-dialog 
    :visible.sync="visible" 
    title="项目详情"
    width="500px"
    @close="handleClose">
        <el-form ref="ruleForm" :rules="rules" :model="formData" label-width="100px">
            <el-form-item label="项目名称" prop="pname">
                <el-input v-model="formData.pname"></el-input>
            </el-form-item>
            <el-form-item label="所属部门" prop="deptno">
                <el-select v-model="formData.deptno" placeholder="请选择部门" clearable style="width: 100%;">
                        <el-option
                        v-for="item in deptData"
                        :key="item.deptno"
                        :label="item.dname"
                        :value="item.deptno"
                        ></el-option>
                    </el-select>
            </el-form-item>
            <el-form-item label="预估产值">
                <el-input-number v-model="formData.ouputvalue" :precision="2" :step="0.1" :min="0" style="width: 100%;"></el-input-number>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="handleSubmit">提交</el-button>
                <el-button @click="handleClose">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
export default {
    name: 'deptInfo',
    props: {
        visible: Boolean,
        deptData: Array,
        projectData: Object
    },
    data() {
        return {
            formData: {
                pid: null,
                pname: '',
                deptno: null,
                dname: '',
                ouputvalue: 0,
                estimatevalue: 0,
            },
            rules: {
                pname: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
                deptno:[{ required: true, message: '请选择所属部门', trigger: 'change' }]
            }
        }
    },
    watch: {
        projectData: {
            handler(newVal) {
                if (newVal) {
                    this.loadFormData()
                }
            }
        }
    },
    methods: {
        loadFormData() {
            this.formData = this.projectData
        },

        handleSubmit(){
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    if(this.formData.pid == null){
                        this.formData.pid = 0
                        this.formData.estimatevalue = this.formData.ouputvalue * 0.4
                        this.$http.post('/addProject', this.formData).then(res => {
                            if(res.data == 'success') {
                                this.$message.success('添加成功')
                                this.handleClose()
                            } else {
                                this.$message.error('添加失败')
                            }
                        }).catch(() => {
                            this.$message.warning('系统异常')
                        })
                    } else {
                        this.formData.estimatevalue = this.formData.ouputvalue * 0.4
                        this.$http.post('/editProject', this.formData).then(res => {
                            if(res.data == 'success') {
                                this.$message.success('更新成功')
                                this.handleClose()
                            } else {
                                this.$message.error('更新失败')
                            }
                        }).catch(() => {
                            this.$message.warning('系统异常')
                        })
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            })
        },

        handleClose() {
            this.$refs['ruleForm'].resetFields()
            this.$emit('close')
        }
    }
}
</script>

<style>

</style>